# -*- coding:utf-8 -*-

import sys, os
from common.import_pyside import *


class DemoFileDialog(QMainWindow):
    def __init__(self, parent=None):
        super(DemoFileDialog, self).__init__(parent)

        # 设置窗口标题
        self.setWindowTitle('实战PyQt5: QFileDialog Demo!')
        # 设置窗口大小
        self.resize(480, 270)

        self.cwd = os.getcwd()

        self.initUi()

    def initUi(self):
        # 为主窗口添加状态条
        self.statusBar = QStatusBar(self)
        self.setStatusBar(self.statusBar)

        # 菜单条
        menuBar = self.menuBar()

        menuFile = menuBar.addMenu('文件')

        # 选择一个文件
        aFileChoose = QAction('选择一个文件...', self)
        aFileChoose.triggered.connect(self.onFileChoose)

        # 选择多个文件
        aFileMultiChoose = QAction('选择多个文件...', self)
        aFileMultiChoose.triggered.connect(self.onFileMultiChoose)

        # 选择文件夹
        aFileChooseDir = QAction('选择一个文件夹...', self)
        aFileChooseDir.triggered.connect(self.onFileChooseDir)

        # 文件保存
        aFileSave = QAction('文件保存...', self)
        aFileSave.triggered.connect(self.onFileSave)

        menuFile.addAction(aFileChoose)
        menuFile.addAction(aFileMultiChoose)
        menuFile.addAction(aFileChooseDir)
        menuFile.addAction(aFileSave)

        # 退出菜单
        aFileExit = QAction('退出', self)
        aFileExit.triggered.connect(self.close)
        menuFile.addSeparator()
        menuFile.addAction(aFileExit)

        self.showInfo = QPlainTextEdit(self)
        self.showInfo.setReadOnly(True)
        self.showInfo.setLineWrapMode(QPlainTextEdit.WidgetWidth)

        self.setCentralWidget(self.showInfo)

    def onFileChoose(self):
        filename, filetype = QFileDialog.getOpenFileName(self, '选择文件',
                                                         self.cwd,  # 起始路径
                                                         'Text Files(*.txt);;All Files(*)')  # 文件扩展名过滤器
        if filename == '':
            self.statusBar.showMessage('取消选择', 2000)
            return

        self.showInfo.appendPlainText('你选择的文件为：{}\n文件筛选器类型为：{}'.format(filename, filetype))

    def onFileMultiChoose(self):
        filenames, filetye = QFileDialog.getOpenFileNames(self,
                                                          '多文件选择',
                                                          self.cwd,
                                                          'PDF Files (*.pdf);;Text Files (*.txt);;All Files (*.*)')
        if len(filenames) == 0:
            self.statusBar.showMessage('取消选择', 2000)
            return
        self.showInfo.appendPlainText('你选择的文件为：')
        for filename in filenames:
            self.showInfo.appendPlainText(filename)
        self.showInfo.appendPlainText('文件筛选器类型为: ' + filetye)

    def onFileChooseDir(self):
        dir_choose = QFileDialog.getExistingDirectory(self,
                                                      '选择文件夹',
                                                      self.cwd)
        if dir_choose == '':
            self.statusBar.showMessage('取消选择', 2000)
            return
        self.showInfo.appendPlainText('你选择的文件夹为：{}'.format(dir_choose))

    def onFileSave(self):
        filename, filetype = QFileDialog.getSaveFileName(self,
                                                         '文件保存',
                                                         self.cwd,
                                                         'PDF Files (*.pdf);;Text Files (*.txt);;All Files (*.*)')
        if filename == '':
            self.statusBar.showMessage('取消选择', 2000)
            return
        self.showInfo.appendPlainText('你选择的保存文件为：{}'.format(filename))
        self.showInfo.appendPlainText('文件筛选器类型：' + filetype)


if __name__ == '__main__':
    app = QApplication(sys.argv)
    window = DemoFileDialog()
    window.show()
    sys.exit(app.exec())
